let config = require('./config.js');
let util = {
	//获取资源文件路径
	getResourceUrl(url) {
		let flag = url.startsWith("/");
		if (flag) {
			url = config.baseUrl + url;
		}
		return url;
	},
	//点击节流事件
	throttle(fn, gapTime) {
		if (gapTime == null || gapTime == undefined) {
			gapTime = 1500
		}
		let _lastTime = null
		// 返回新的函数
		return function() {
			let _nowTime = +new Date()
			if (_nowTime - _lastTime > gapTime || !_lastTime) {
				fn.apply(this, arguments) //将this和参数传给原函数
				_lastTime = _nowTime
			}
		}
	},
	showType(viewType) {
		let obj = {};
		if (viewType == 'water') {
			obj = {
				lable: '水量',
				unit: '吨'
			}
		} else {
			obj = {
				lable: '气量',
				unit: '方'
			}
		}
		return obj;
	},
	//moneyFlag，是否金额，viewType 显示类型，
	showTag(moneyFlag, viewType) {
		let obj = {}
		if (moneyFlag == '1') {
			obj = {
				lable: '金额',
				unit: '元'
			}
		} else {
			if (viewType == 'water') {
				obj = {
					lable: '水量',
					unit: '吨'
				}
			} else {
				obj = {
					lable: '气量',
					unit: '方'
				}
			}
		}
		return obj;
	},
	//获取当前时间
	getNowTime: function() {
		let date = new Date();
		this.year = date.getFullYear();
		this.month = date.getMonth() + 1;
		this.date = date.getDate();
		this.month = this.month < 10 ? "0" + this.month : this.month;
		this.date = this.date < 10 ? "0" + this.date : this.date;
		this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
		this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
		this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
		let currentTime = this.year + this.month + this.date + this.hour + this.minute + this.second;
		return currentTime;
	},
	//获取格式化时间
	formateByTime: function(time) {
		if (typeof time == "undefined") return;
		let newTime = time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(
			8, 10) + ":" + time.substring(10, 12) + ":" + time.substring(12, 14);
		return newTime;
	},
	//获取格式化时间
	formateByDate: function(time) {
		if (typeof time == "undefined") return;
		let date = time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8);
		return date;
	},

	//showModal
	showModal: function(content, callback = function() {}, showCancel = false, confirmText = "确定") {
		uni.showModal({
			title: '提示',
			content: content,
			showCancel: showCancel,
			confirmText: confirmText,
			success(res) {
				if (res.confirm) {
					callback();
				}
			}
		});
	},
	//跳转
	reLaunch: function(url) {
		uni.reLaunch({
			url: url
		});
	},
	//跳转
	navigateTo: function(url) {
		uni.navigateTo({
			url: url
		});
	},
	//设置缓存数据
	setStorage: function(key, value) {
		return new Promise((resolve, reject) => {
			uni.setStorage({
				key: key,
				data: value
			})
			resolve();
		});
	},
	//获取缓存数据
	getStorage: function(key) {
		return new Promise((resolve, reject) => {
			uni.getStorage({
				key: key,
				success(res) {
					resolve(res.data);
				},
				fail(res) {
					resolve(0);
				}
			})
		})
	},
	unique(arr){
		return Array.from(new Set(arr)); // 利用Array.from将Set结构转换成数组
	},
	//去除重复的值
	setSplitNumber(list, prop) {
		let num = 5;
		let arr = [];
		for (let i = 0; i < list.length; i++) {
			arr.push(list[i][prop]);
		}
		let newArr = this.unique(arr);
		let len = newArr.length;
		if(len < 5){
			num = len;
		}
		return num;
	},
	//显示loading
	showLoading: function(content) {
		uni.showLoading({
			title: content,
			mask: true
		})
	},
	//获取地址栏参数
	getQueryString: function(name) {
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
		var r = window.location.search.substr(1).match(reg);
		if (r != null) {
			return unescape(r[2]);
		}
		return null;
	}
}
module.exports = util;
